//
//  MIneListHomeViewController.m
//  Mahjong
//
//  Created by KING on 16/10/20.
//  Copyright © 2016年 swift. All rights reserved.
//

#import "MIneListHomeViewController.h"
#import "MoreSelectView.h"
#import "Header.h"
#import "MineListNumberTableViewCell.h"
#import "MineListAdressTableViewCell.h"
#import "MineListConTableViewCell.h"
#import "MineListDetailViewController.h"
#import "MineListTwoDetailViewController.h"
#import "MineListAlreDetailViewController.h"
#import "MineListUnDetailViewController.h"
#import "MIneListEvaluationDetailViewController.h"
#import "ListHomeModel.h"
@interface MIneListHomeViewController ()
<selectDelegate,UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *bigScrollView;
@property (nonatomic, strong) MoreSelectView *selectView;
@property (nonatomic, strong) NSArray *topArray;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, strong) NSMutableArray *dateArray;

@end

@implementation MIneListHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 42, SCREEN_WIDTH, SCREENH_HEIGHT-166) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerNib:[UINib  nibWithNibName:@"MineListNumberTableViewCell" bundle:nil] forCellReuseIdentifier:@"numbercell"];
    self.dateArray = [NSMutableArray array];

    [self.myTableView registerNib:[UINib nibWithNibName:@"MineListAdressTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerNib:[UINib  nibWithNibName:@"MineListConTableViewCell" bundle:nil] forCellReuseIdentifier:@"concell"];
    [self addTopView];
    [self create:0];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dateArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ListHomeModel *model = self.dateArray[indexPath.section];
    if (indexPath.row == 0) {
        MineListNumberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"numbercell"];
        cell.titleLabel.text = [NSString stringWithFormat:@"%ld",model.OrderId];

        cell.conLabel.text = model.addtime;
        return cell;
    }if (indexPath.row == 1) {
        MineListAdressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.name.text = model.khname;
        cell.tel.text = model.khtel;
        cell.address.text = model.khaddress;
        return cell;
        
    } else {
        MineListConTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"concell"];
        cell.mian.text = model.size;
        return cell;
        
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 1){
        return 75;
    }
    return 40;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.number == 100) {
        MineListDetailViewController *detail = [[MineListDetailViewController alloc] init];
    
        detail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detail animated:YES];
    }
    if (self.number == 101) {
        MineListTwoDetailViewController *detail = [[MineListTwoDetailViewController alloc] init];
        detail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detail animated:YES];
    } if (self.number == 102) {
        MineListAlreDetailViewController *detail = [[MineListAlreDetailViewController alloc] init];
        detail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detail animated:YES];
    } if (self.number == 103) {
        MineListUnDetailViewController *detail = [[MineListUnDetailViewController alloc] init];
        detail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detail animated:YES];
    } if (self.number == 104) {
        MIneListEvaluationDetailViewController *detail = [[MIneListEvaluationDetailViewController alloc] init];
        detail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:detail animated:YES];
    }
    
    
}
-(void)oneSelectAction{
    
}
-(void)twoSelectAction{
    
}
-(void)create:(NSInteger)num{
    
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=GetCustomizedList&page=1&userid=%@&state=%ld",[[NSUserDefaults standardUserDefaults] objectForKey:@"userid"],num] success:^(id result) {
        for (NSDictionary *dic in [result[0] objectForKey:@"date"]) {
            ListHomeModel *model = [[ListHomeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dateArray addObject:model];
        }
        
        [self.myTableView reloadData];
        
    } error:^(NSError *error) {
        
        
    }];
    
}
-(void)addTopView{
    self.titleArray = @[@"待受理",@"待确认",@"已报价",@"待完成",@"未评价"];
    for (int i = 0; i <5; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*SCREEN_WIDTH/5, 2, SCREEN_WIDTH/5, 42);
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.view addSubview:button];
        button.tag = 100+i;
        [button addTarget:self action:@selector(topAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    UIButton *a = (UIButton *)[self.view viewWithTag:100];
    [a setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.number = 100;
}
-(void)topAction:(UIButton *)btn{
    if (btn.tag == 100) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 100;
        [self create:0];
        
    }
    if (btn.tag == 101) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 101;
        [self create:1];

        
    }
    if (btn.tag == 102) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 102;
        [self create:2];

        
    }if (btn.tag == 103) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.number = 103;
        [self create:3];

        
    }
    if (btn.tag == 104) {
        UIButton *a = (UIButton *)[self.view viewWithTag:100];
        [a setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *b = (UIButton *)[self.view viewWithTag:101];
        [b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *c = (UIButton *)[self.view viewWithTag:102];
        [c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *d = (UIButton *)[self.view viewWithTag:103];
        [d setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        UIButton *e = (UIButton *)[self.view viewWithTag:104];
        [e setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        self.number = 104;
        [self create:4];

        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
